package com.open.capacity.user.service.impl;

import com.open.capacity.commons.Result;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.user.dao.SysAuthorizeDao;
import com.open.capacity.user.enums.AuthorizeItemTypes;
import com.open.capacity.user.model.SysAuthorize;
import com.open.capacity.user.service.SysAuthorizeService;
import com.open.capacity.user.vo.SysAuthorizeVo;
import com.open.capacity.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class SysAuthorizeServiceImpl implements SysAuthorizeService {

    @Autowired
    private SysAuthorizeDao authorizeDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(SysAuthorize sysAuthorize) {
        sysAuthorize.setCreateTime(new Date());

        authorizeDao.save(sysAuthorize);
        log.info("保存功能授权：{}", sysAuthorize);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByObjectId(String objectId) {

        authorizeDao.deleteByObjectId(objectId);

        log.info("删除功能授权：{}", objectId);
    }


    @Override
    public List<SysAuthorize> findByObjectId(String objectId) {
        return authorizeDao.findByObjectId(objectId);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result saveOrUpdate(SysAuthorizeVo sysAuthorize) {
        int i = 0;
        //authorizeDao.deleteByObjectId(sysAuthorize.getObjectId());

        List<SysAuthorize> origList = authorizeDao.findByObjectId(sysAuthorize.getObjectId());
        if (!CollectionUtils.isEmpty(origList)) {
            List<String> deleteMenuIds = origList.stream().filter(item -> item.getItemType().equals(1)).map(SysAuthorize::getItemId).filter(item -> !sysAuthorize.getMenus().contains(item)).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(deleteMenuIds)) {
                authorizeDao.deleteByIds(sysAuthorize.getObjectId(), 1, deleteMenuIds);
            }

            List<String> deleteBtnIds = origList.stream().filter(item -> item.getItemType().equals(2)).map(SysAuthorize::getItemId).filter(item -> !sysAuthorize.getBtns().contains(item)).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(deleteBtnIds)) {
                authorizeDao.deleteByIds(sysAuthorize.getObjectId(), 2, deleteBtnIds);
            }

            List<String> deleteColIds = origList.stream().filter(item -> item.getItemType().equals(3)).map(SysAuthorize::getItemId).filter(item -> !sysAuthorize.getCols().contains(item)).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(deleteColIds)) {
                authorizeDao.deleteByIds(sysAuthorize.getObjectId(), 3, deleteColIds);
            }
        }

        Date now = new Date();
        if (!CollectionUtils.isEmpty(sysAuthorize.getMenus())) {
            List<String> addMenuIds = sysAuthorize.getMenus().stream().filter(item -> !origList.stream().filter(a -> a.getItemType().equals(1)).map(SysAuthorize::getItemId).collect(Collectors.toList()).contains(item)).collect(Collectors.toList());
            addMenuIds.forEach(m -> {
                SysAuthorize model = new SysAuthorize();
                model.setId(UUIDUtils.getOriginalUUIDWithoutBar());
                model.setObjectType(sysAuthorize.getObjectType());
                model.setObjectId(sysAuthorize.getObjectId());
                model.setItemType(AuthorizeItemTypes.MENU.getCode());
                model.setItemId(m);
                model.setStatus(-1);
                model.setCreateTime(now);
                authorizeDao.save(model);
            });
        }
        if (!CollectionUtils.isEmpty(sysAuthorize.getBtns())) {
            List<String> addBtnIds = sysAuthorize.getBtns().stream().filter(item -> !origList.stream().filter(a -> a.getItemType().equals(2)).map(SysAuthorize::getItemId).collect(Collectors.toList()).contains(item)).collect(Collectors.toList());
            addBtnIds.forEach(b -> {
                SysAuthorize model = new SysAuthorize();
                model.setId(UUIDUtils.getOriginalUUIDWithoutBar());
                model.setObjectType(sysAuthorize.getObjectType());
                model.setObjectId(sysAuthorize.getObjectId());
                model.setItemType(AuthorizeItemTypes.BUTTON.getCode());
                model.setItemId(b);
                model.setStatus(-1);
                model.setCreateTime(now);
                authorizeDao.save(model);
            });
        }
        if (!CollectionUtils.isEmpty(sysAuthorize.getCols())) {
            List<String> addColIds = sysAuthorize.getCols().stream().filter(item -> !origList.stream().filter(a -> a.getItemType().equals(3)).map(SysAuthorize::getItemId).collect(Collectors.toList()).contains(item)).collect(Collectors.toList());
            addColIds.forEach(c -> {
                SysAuthorize model = new SysAuthorize();
                model.setId(UUIDUtils.getOriginalUUIDWithoutBar());
                model.setObjectType(sysAuthorize.getObjectType());
                model.setObjectId(sysAuthorize.getObjectId());
                model.setItemType(AuthorizeItemTypes.COLUMN.getCode());
                model.setItemId(c);
                model.setStatus(-1);
                model.setCreateTime(now);
                authorizeDao.save(model);
            });
        }


        return 1 > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
    }
}
